package pearson.automation.forms.courseDetails.module;

import java.util.List;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import framework.BaseForm;
import framework.Browser;
import framework.elements.Button;
import framework.elements.CheckBox;
import framework.elements.ElementList;

/**
 * 
 * This form represents Objectives module's content part
 * 
 */
public class ObjectivesForm extends BaseForm {

    private final static String titlexPath = "//div[contains(@class,'section_area')][contains(.,'%1$s')]";
    private final Button addToModules;
    private final ElementList moduleList;
    private final String checkboxLocator;

    public ObjectivesForm(String name) {
    super(By.xpath(String.format(titlexPath, name)), name);
    String title = String.format(titlexPath, name);
    moduleList = new ElementList(By.xpath(String.format(
        "%1$s//div[contains(@class,'lo-assign')]//li/span[contains(@class,'number-replace')]", title)),
        "Modules");
    addToModules = new Button(By.xpath(String.format("%1$s//h2[text()='Add to modules']", title)), "Add to modules");
    checkboxLocator = String
        .format("%1$s//div[contains(@class,'lo-assign')]//li[contains(.,'%2$s')]//div[contains(@class,'custom-checkbox-main-wrapper')]",
            title, "%1$s");

    }

    public void collapse() {
    if (!addToModules.getElement()
            .getAttribute("class").toLowerCase().contains("closed")) {
        addToModules.clickAndWait();
    }
    try {
        Thread.sleep(Integer.parseInt(Browser.props.getProperty("speedValue")) * 3);
    } catch (InterruptedException e) {
    }
    Assert.assertTrue(addToModules.getElement().getAttribute("class").toLowerCase().contains("closed"),
        "Modules doesn't collapse");
    }

    public void expand() {
    if (addToModules.getElement()
            .getAttribute("class").toLowerCase().contains("closed")) {
        addToModules.clickAndWait();
    }
    try {
        Thread.sleep(Integer.parseInt(Browser.props.getProperty("speedValue")) * 3);
    } catch (InterruptedException e) {
    }
    Assert.assertTrue(!addToModules.getElement().getAttribute("class").toLowerCase().contains("closed"),
        "Modules doesn't collapse");
    }

    public void checkModule(int moduleIndex) {
    expand();
    String name = moduleList.getListElement(moduleIndex);
    CheckBox module = new CheckBox(By.xpath(String.format(checkboxLocator, name)), name);
    module.check();
    }

    public boolean getModuleState(int moduleIndex) {
    expand();
    String name = moduleList.getListElement(moduleIndex);
    CheckBox module = new CheckBox(By.xpath(String.format(checkboxLocator, name)), name);
    return module.isChecked();
    }

    public boolean getModuleModifyState(int moduleIndex) {
    expand();
    String name = moduleList.getListElement(moduleIndex);
    CheckBox module = new CheckBox(By.xpath(String.format(checkboxLocator, name)), name);
    return module.isEnabled();
    }

    public void assertModuleCheckState(int[] checked, int[] unchecked) {
    for (int state : checked) {
        Assert.assertTrue(getModuleState(state), String.format("Module %1$s is unchecked", state + 1));
    }

    for (int state : unchecked) {
        Assert.assertFalse(getModuleState(state), String.format("Module %1$s is checked", state + 1));
    }
    }

    public void assertModuleEnablibgState(int[] enabled, int[] disabled) {
    for (int state : enabled) {
        Assert.assertTrue(getModuleModifyState(state), String.format("Module %1$s is disabled", state + 1));
    }

    for (int state : disabled) {
        Assert.assertFalse(getModuleModifyState(state), String.format("Module %1$s is enabled", state + 1));
    }
    }

    public String[] getModulesList() {
    List<WebElement> modules = moduleList.getListElements();
    String[] names = new String[modules.size()];
    for (int i = 0; i < names.length; i++) {
        names[i] = modules.get(i).getText();
    }
    return names;
    }
}
